package com.ld.control.business.equipment.controller;


import com.ld.control.business.equipment.service.EquipmentService;
import com.ld.control.business.equipment.vo.req.EquipmentReqVo;
import com.ld.control.business.equipment.vo.resp.EquipmentRespVo;
import com.ld.control.common.vo.GenerateListVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.common.ResultResponse;
import com.ld.control.sysenum.ResultResponseEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 设备表 前端控制器
 * </p>
 *
 * @author wangfeng
 * @since 2024-04-17
 */
@Slf4j
@Tag(name = "设备相关接口")
@RestController
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @GetMapping("/api/centralControl/equipment/getPageList")
    @Operation(summary = "设备列表")
    public ResultResponse<GeneratePageListVo<EquipmentRespVo>> getPageList(
            @Parameter(description = "设备区域Id") @RequestParam(required = false) Long regionId,
            @Parameter(description = "设备名称") @RequestParam(required = false) String name,
            @Parameter(description = "设备编号") @RequestParam(required = false) String code,
            @Parameter(description = "设备状态，0离线，1模拟在线，2,真实在线，3运行中，4异常，5错误") @RequestParam(required = false) Integer status,
            @Parameter(description = "是否可用：0否，1是") @RequestParam(required = false) Integer useFlag,
            @RequestParam(required = false, defaultValue = "1") Integer current,
            @RequestParam(required = false, defaultValue = "10") Integer size
    ) {
        GeneratePageListVo<EquipmentRespVo> equipmentServicePageList = equipmentService.getPageList(regionId, name, code, status, useFlag, current, size);
        return ResultResponse.success(equipmentServicePageList);
    }

    @GetMapping("/api/centralControl/equipment/getById")
    @Operation(summary = "查询设备详情")
    public ResultResponse<EquipmentRespVo> getById(@RequestParam Long id) {
        EquipmentRespVo equipmentRespVo = equipmentService.getById(id);
        return ResultResponse.success(equipmentRespVo);
    }

    @PostMapping("/api/centralControl/equipment/add")
    @Operation(summary = "添加设备")
    public ResultResponse<ResultResponseEnum> add(@RequestBody EquipmentReqVo reqVo) {
        equipmentService.add(reqVo);
        return ResultResponse.success();
    }

    @PostMapping("/api/centralControl/equipment/update")
    @Operation(summary = "修改设备", description = "状态是运行中的设备不能修改")
    public ResultResponse<ResultResponseEnum> update(@RequestBody EquipmentReqVo reqVo) {
        equipmentService.update(reqVo);
        return ResultResponse.success();
    }

    @PostMapping("/api/centralControl/equipment/deleteById")
    @Operation(summary = "删除设备", description = "状态是运行中的设备不能删除")
    public ResultResponse<ResultResponseEnum> deleteById(@RequestParam(required = false) Long id) {
        equipmentService.deleteById(id);
        return ResultResponse.success();
    }
    @GetMapping("/api/centralControl/equipment/getRunMethodEquipment")
    @Operation(summary = "获取某个任务实例下面执行了runMethod的设备", description = "获取某个任务实例下面执行了runMethod的设备")
    public ResultResponse<GenerateListVo<EquipmentRespVo>> getRunMethodEquipment(@RequestParam Long taskExampleId) {
        List<EquipmentRespVo> methodEquipmentList = equipmentService.getRunMethodEquipment(taskExampleId);
        return ResultResponse.success(new GenerateListVo<>(methodEquipmentList));
    }

    @GetMapping("/api/centralControl/equipment/getAllList")
    @Operation(summary = "获取所有设备信息列表", description = "获取所有设备信息列表")
    public ResultResponse<GenerateListVo<EquipmentRespVo>> getAllList() {
        return ResultResponse.success(equipmentService.getAllList());
    }

}

